package com.yedean.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.entity.UmsResourceCategory;
import com.yedean.mall.service.IUmsResourceCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台资源分类管理Controller
 *
 * @author yedean
 * @date 2022/8/9
 */
@Controller
@Api(tags = "后台资源分类管理")
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {
    @Autowired
    private IUmsResourceCategoryService resourceCategoryService;


    @ApiOperation("查询所有后台资源分类")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsResourceCategory>> listAll() {
        LambdaQueryWrapper<UmsResourceCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(UmsResourceCategory::getSort);
        List<UmsResourceCategory> resourceList = resourceCategoryService.list(wrapper);
        return CommonResult.success(resourceList);
    }

    @ApiOperation("添加后台资源分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> create(@RequestBody UmsResourceCategory umsResourceCategory) {
        boolean save = resourceCategoryService.save(umsResourceCategory);
        if (save) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台资源分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@PathVariable Long id,
                                  @RequestBody UmsResourceCategory umsResourceCategory) {


        QueryWrapper<UmsResourceCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        boolean update = resourceCategoryService.update(umsResourceCategory, wrapper);
        if (update) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID删除后台资源")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> delete(@PathVariable Long id) {
        boolean remove = resourceCategoryService.removeById(id);
        if (remove) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

}
